home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
dm3_src.zip
/
DMTIME.C
< prev
next >
Wrap
Text File
|
1990-08-02
|
17KB
|
547 lines
/* ************************************************************************* */
/* */
/* D O O R W A R E D A T E & T I M E L I B R A R Y */
/* */
/* For Mycrosoft & Turbo C */
/* */
/* ************************************************************************* */
/* */
/* This module contains functions used to input and output info */
/* with the user and remote_users console. Additionally, it */
/* contains functions that interface with DOS. */
/* */
/* The following funtions are contained in this file: */
/* */
/* dmtimer_open - Access timer hardware */
/* dmtimer_close - Release timer hardware */
/* dmdelay - Delay function */
/* dmsleep - Delay function */
/* get_date - Get current date in days */
/* cvt_days - Convert date string to day count */
/* get_time - Get current time in seconds */
/* cvt_time - Convert time string to seconds */
/* test_timeout - Test if user out of time */
/* */
/* */
/* ************************************************************************* */
/* ************************************************************************* */
/* PROGRAM HISTORY */
/* ************************************************************************* */
/* 06/20/87 Version 1.00 */
/* */
/* */
/* */
/* */
/* ************************************************************************* */
#include "dmcfg.h" /* Std. defines & includes */
#include "dmdata.h" /* Std DM storage */
/*
*
* Structure Definitions
*
*/
struct dmtime
{
int type; /* Timer type */
/* 0 = Multiple Event Interval Timer */
/* 1 = Single Event Countdown Timer */
unsigned int counter; /* Tick Counter */
unsigned int interval; /* Interval or Countdown Value */
unsigned int event; /* Event Flag/Counter */
char *link; /* Link to next timer */
};
#define DMTIMER struct dmtime
/*
*
* Local module storage
*
*/
static int tm_init_state = 0;
static int tm_open_state = 0;
static int time_warning = 1;
/*
*
* Access Timer Hardware
*
*/
dmtimer_open()
{
#if COMPILER == MSC
onexit_t onexit(); /* Onexit declaration */
#endif
#if COMPILER == BTC
int atexit();
#endif
void dmtimer_close();
if(tm_open_state == 0)
{
dmtimer_init(); /* Access the hardware */
if(tm_init_state == 0)
{
#if COMPILER == MSC
onexit(dmtimer_close);
#endif
#if COMPILER == BTC
atexit(dmtimer_close);
#endif
tm_init_state = 1;
}
tm_open_state = 1;
}
return(0); /* Always returns 0 */
}
/*
*
* Release Timer Hardware
*
*/
void dmtimer_close()
{
if(tm_open_state)
{
dmtimer_release(); /* Release the hardware */
tm_open_state = 0;
}
return; /* Always returns 0 */
}
/*
*
* Sleep n Seconds
*
*/
dmsleep(interval)
int interval; /* Time in 1/10th seconds */
{
dmdelay(interval * 10); /* Execute the delay */
return(0); /* All done, no errors */
}
/*
*
* Sleep n 1/10th Seconds
*
*/
dmdelay(interval)
int interval; /* Time in 1/10th seconds */
{
DMTIMER sleep_timer; /* Timer structure */
modem_wait(); /* Wait for tx buff empty */
sleep_timer.type = 1; /* Type is countdown timer */
sleep_timer.counter = interval; /* Init count */
sleep_timer.interval = interval; /* Init the interval value */
sleep_timer.event = 0; /* Flag as no event */
dmtimer_add(&sleep_timer); /* Add the block to chain */
while(sleep_timer.event == 0); /* Until timeout */
dmtimer_sub(&sleep_timer); /* Release resource */
return(0); /* All done, no errors */
}
/*
*
* Get current date
*
*/
get_date(date_string)
char *date_string;
{
long time(); /* Get time function */
long cvt_time(); /* Convert time declaration */
char *ctime(); /* Current time string */
long time_data; /* Current time */
char *time_str; /* Time string */
time(&time_data); /* Get the current time */
time_str = ctime(&time_data); /* Convert to local time */
date_string[0] = time_str[4]; /* Copy month */
date_string[1] = time_str[5];
date_string[2] = time_str[6];
date_string[3] = time_str[7];
date_string[4] = time_str[8]; /* Copy date */
date_string[5] = time_str[9];
date_string[6] = time_str[10];
date_string[7] = time_str[20]; /* Copy year */
date_string[8] = time_str[21];
date_string[9] = time_str[22];
date_string[10] = time_str[23];
date_string[11] = '\0'; /* Terminate string */
return(0); /* Exit, no erros */
}
/*
*
* Convert date string to days since Jan 1 0000
*
*/
long cvt_days(date_str)
char *date_str; /* Date string */
{
long atol(); /* Ascii to long */
long day_count; /* Total days */
char mo_str[4]; /* Month string */
char day_str[3]; /* Day string */
char year_str[5]; /* Year string */
mo_str[0] = date_str[0]; /* Copy the month section */
mo_str[1] = date_str[1];
mo_str[2] = date_str[2];
mo_str[3] = '\0';
day_str[0] = date_str[4]; /* Copy the minutes section */
day_str[1] = date_str[5];
day_str[2] = '\0';
year_str[0] = date_str[7]; /* Copy the seconds section */
year_str[1] = date_str[8];
year_str[2] = date_str[9];
year_str[3] = date_str[10];
year_str[4] = '\0';
if(strcmp("JAN", strupr(mo_str)) == 0) /* Jan = month 0 */
day_count = 0L;
else if(strcmp("FEB", strupr(mo_str)) == 0) /* Feb = month 1 */
day_count = 31L;
else if(strcmp("MAR", strupr(mo_str)) == 0) /* Mar = month 2 */
day_count = 59L;
else if(strcmp("APR", strupr(mo_str)) == 0) /* Apr = month 3 */
day_count = 90L;
else if(strcmp("MAY", strupr(mo_str)) == 0) /* May = month 4 */
day_count = 120L;
else if(strcmp("JUN", strupr(mo_str)) == 0) /* Jun = month 5 */
day_count = 151L;
else if(strcmp("JUL", strupr(mo_str)) == 0) /* Jul = month 6 */
day_count = 181L;
else if(strcmp("AUG", strupr(mo_str)) == 0) /* Aug = month 7 */
day_count = 212L;
else if(strcmp("SEP", strupr(mo_str)) == 0) /* Sep = month 8 */
day_count = 243L;
else if(strcmp("OCT", strupr(mo_str)) == 0) /* Oct = month 9 */
day_count = 273L;
else if(strcmp("NOV", strupr(mo_str)) == 0) /* Nov = month 10 */
day_count = 304L;
else if(strcmp("DEC", strupr(mo_str)) == 0) /* Dec = month 11 */
day_count = 334L;
day_count += atol(day_str) * 1L;
day_count += atol(year_str) * 365L;
return(day_count); /* Exit, with count in days */
}
/*
*
* Get number of seconds past midnight
*
*/
long cur_time()
{
long time(); /* Get time function */
long cvt_time(); /* Convert time declaration */
char *ctime(); /* Current time string */
long time_data; /* Current time */
char *time_str; /* Time string */
time(&time_data); /* Get the current time */
time_str = ctime(&time_data); /* Convert to local time */
time_data = cvt_time(&time_str[11]); /* Convert to seconds */
return(time_data); /* Exit, with time in secs. */
}
/*
*
* Convert time string to seconds past midnight
*
*/
long cvt_time(time_str)
char *time_str; /* Time string */
{
long atol(); /* Ascii to long */
long time_data; /* Current time */
char hour_str[3]; /* Hours string */
char minu_str[3]; /* Minutes string */
char seco_str[3]; /* Seconds string */
hour_str[0] = time_str[0]; /* Copy the hours section */
hour_str[1] = time_str[1];
hour_str[2] = '\0';
minu_str[0] = time_str[3]; /* Copy the minutes section */
minu_str[1] = time_str[4];
minu_str[2] = '\0';
seco_str[0] = time_str[6]; /* Copy the seconds section */
seco_str[1] = time_str[7];
seco_str[2] = '\0';
time_data = atol(hour_str) * 3600L; /* Convert to seconds */
time_data += atol(minu_str) * 60L;
time_data += atol(seco_str) * 1L;
return(time_data); /* Exit, with time in secs. */
}
/*
*
* Test for timeout condition
*
*/
test_timeout()
{
long cur_time(); /* Get current time decl. */
long cctime; /* Current_time */
if(remote_user)
{
cctime = cur_time(); /* Get the time */
if(cctime < user_start) /* If we wrapped past 12... */
cctime += DAY_SECONDS; /* ...adjust it */
if(cctime > user_signoff) /* If time used up... */
return(-2); /* ...alert caller */
if(time_warning) /* If we are checking... */
{
if(cctime > (user_signoff - 300L)) /* If less than 5 minutes... */
{
time_warning = 0; /* ...clip the flag */
return(-1); /* ...alert caller */
}
}
}
return(0); /* Exit, with no error */
}
/*
*
* Load time control file & test controls
*
*/
time_controls(timefile)
char *timefile;
{
FS rfd;
long cvt_time();
long end_time;
int i, j, k;
/*
* Sysop default hours
*/
bbs_time_info.sysop_start = cvt_time("00:00:00");
bbs_time_info.sysop_stop = cvt_time("23:59:59");
user_access_level = -1; /* assume not used */
/*
* Open up the time file
*/
strcpy(rfd.name, timefile);
i = file_open(&rfd, FREAD, FBINARY, FNOCREATE); /* Open time file */
if(i) /* If it does not exist */
return(0); /* ...exit with error */
/*
* Process file info
*/
if(fread(&bbs_time_info, 1, sizeof(struct bbs_time), rfd.fd) !=
sizeof(struct bbs_time)) /* If bad read... */
{
file_close(&rfd); /* ...close file */
return(-1); /* ... exit with error */
}
file_close(&rfd);
/*
* Test if security high enough
*/
i = -1;
j = 0;
while((i < 7) && (j == 0))
{
if(user_security >= bbs_time_info.levels[i + 1])
i++;
else
j = 1;
}
if(i == -1) /* error if too low of secur.*/
return(-2);
user_access_level = i; /* Store access level */
/*
* Adjust time limit
*/
j = 0;
while((j < 7) && (user_start >= bbs_time_info.access[j + 1].start_time) &&
(bbs_time_info.access[j + 1].start_time != 0L))
{
j++;
}
if(bbs_time_info.access[j].levels[i] == 0L)
return(-3); /* Not allowed now */
end_time = bbs_time_info.access[j].levels[i] * 60L;
end_time += user_start;
if(end_time < user_signoff)
user_signoff = end_time;
return(1);
}
/*
*
* Test daily time controls
*
*/
daily_controls()
{
long cur_time();
long cvt_days();
long today_date;
long last_date;
long now_time;
long end_time;
int i, j, k;
char date[12];
/*
* Test if controls are active
*/
if(user_access_level >= 0)
{
/*
* Test if user played today
*/
get_date(date);
today_date = cvt_days(date);
last_date = cvt_days(user_game_date);
if(today_date != last_date)
{
get_date(user_game_date); /* set today as last date */
user_day_time = 0L;
user_day_games = 0;
return(0); /* exit all ok */
}
/*
* Test if user waited long enough since last game
*/
if(bbs_time_info.wait[user_access_level] != 0)
{
now_time = cur_time();
end_time = user_game_end + (bbs_time_info.wait[user_access_level] * 60L);
if(now_time < end_time)
return(-1); /* error wait longer */
}
/*
* Test if user played too many games today
*/
if(bbs_time_info.dayg[user_access_level] != 0)
{
if(user_day_games >= bbs_time_info.dayg[user_access_level])
return(-2); /* error too many games */
}
/*
* Test if user played too many minutes today
*/
if(bbs_time_info.dayt[user_access_level] != 0)
{
if(user_day_time >= (bbs_time_info.dayt[user_access_level] * 60L))
return(-3); /* error no more time today */
/*
* Adjust time limit based on daily rate
*/
end_time = (bbs_time_info.dayt[user_access_level] * 60L) - user_day_time;
end_time += user_start;
if(end_time < user_signoff)
user_signoff = end_time;
}
}
return(0);
}